import java.util.*;

class ListNode{
    public int val;
    public ListNode next;
}

public class test_2024_01_08 {
    public int numberOfBoomerangs(int[][] points) {
        int cnt = 0;
        for(int[] p : points){
            HashMap<Integer, Integer> hash = new HashMap<>();
            for(int[] q : points){
                int dis = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]);
                hash.put(dis, hash.getOrDefault(dis, 0) + 1);
            }
            for(Map.Entry<Integer, Integer> entry : hash.entrySet()){
                cnt += (entry.getValue() * (entry.getValue() - 1));
            }
        }
        return cnt;
    }


    public int countGoodStrings(int low, int high, int zero, int one) {
        int MOD = 1000000007;
        int[] dp = new int[high + 1];
        dp[0] = 1;
        for(int i = 1; i <= high; i++){
            if(i >= zero){
                dp[i] = (dp[i] + dp[i - zero]) % MOD;
            }
            if(i >= one){
                dp[i] = (dp[i] + dp[i - one]) % MOD;
            }
        }
        int ans = 0;
        for(int i = low; i <= high; i++){
            ans = (ans + dp[i]) % MOD;
        }
        return ans;
    }

    public int minLength(String s) {
        Stack<Character> stack = new Stack<>();
        char[] arr = s.toCharArray();
        for(Character ch : arr){
            if(stack.empty()){
                stack.push(ch);
            }else{
                Character tmp = stack.peek();
                if(ch == 'D' && tmp == 'C'){
                    stack.pop();
                }else if(ch == 'B' && tmp == 'A'){
                    stack.pop();
                }else{
                    stack.push(ch);
                }

            }
        }
        return stack.size();
    }

    public int addMinimum(String word) {
        int i = 0;
        int j = 0;
        int sz = word.length();
        StringBuilder stringBuilder = new StringBuilder();
        for(i = 0; i < sz; i++){
           stringBuilder.append("abc");
        }
        i = 0;
        while(i < 3 * sz && j < sz){
            if(stringBuilder.charAt(i) == word.charAt(j)){
                i++;
                j++;
            }else{
                i++;
            }
        }
        while(i < 3 * sz && stringBuilder.charAt(i) != 'c'){
            i++;
        }
        if(word.charAt(j - 1) == 'c'){
            return i - j;
        }else{
            return i - j + 1;
        }
    }

    public int countWords(String[] words1, String[] words2) {
        HashMap<String, Integer> hash1 = new HashMap<>();
        HashMap<String, Integer> hash2 = new HashMap<>();
        for(String s : words1){
            hash1.put(s, hash1.getOrDefault(s, 0) + 1);
        }
        for(String s : words2){
            hash2.put(s, hash2.getOrDefault(s, 0) + 1);
        }
        int count = 0;
        for(Map.Entry<String, Integer> entry : hash1.entrySet()){
            if(entry.getValue() == 1){
                String key = entry.getKey();
                if(hash2.get(key) != null && hash2.get(key) == 1){
                    count++;
                }
            }
        }
        return count;
    }

    public String repeatLimitedString(String s, int repeatLimit) {
        int[] arr = new int[26];
        char[] arrStr = s.toCharArray();
        for(char ch : arrStr){
            arr[ch - 'a']++;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int i = 0;
        for(i = 25; i >= 0; i--){
           if(arr[i] > 0){
               if(arr[i] <= repeatLimit){
                   for(int j = 0; j < arr[i]; j++){
                       stringBuilder.append((char)(i + 'a'));
                   }
               }else{
                   arr[i] -= repeatLimit;
                   for(int j = 0; j < repeatLimit; j++){
                       stringBuilder.append((char)(i + 'a'));
                   }
                   int flag = 0;
                   for(int j = i - 1; j >= 0; j--){
                       if(arr[j] > 0){
                           arr[j]--;
                           stringBuilder.append((char)(j + 'a'));
                           flag = 1;
                           break;
                       }
                   }
                   if(flag == 0){
                       break;
                   }else{
                       i++;
                   }
               }
           }
        }
        return stringBuilder.toString();
    }

    public ListNode deleteDuplicates1(ListNode head) {
        if(head == null){
            return head;
        }
        ListNode end = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val != end.val){
                end.next = cur;
                end = end.next;
            }
            cur = cur.next;
        }
        end.next = null;
        return head;
    }

    public int integerBreak(int n) {
        int[] dp = new int[n + 1];
        dp[2] = 2;
        if(n == 2){
            return 1;
        }
        if(n == 3){
            return 2;
        }
        dp[3] = 3;
        for(int i = 4; i <= n; i++){
            dp[i] = Math.max(dp[i - 2] * 2, dp[i - 3] * 3);
        }
        for(int e : dp){
            System.out.print("" + e + ",");
        }
        return dp[n];
    }


    public ListNode deleteDuplicates2(ListNode head) {
        ListNode ret = null;
        ListNode end = null;
        ListNode cur = head;
        if(head == null){
            return null;
        }
        while(cur != null){
            if(cur.next != null && cur.val == cur.next.val){
                while(cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                cur = cur.next;
            }else{
                if(end == null){
                    end = cur;
                    ret = end;
                }else{
                    end.next = cur;
                    end = end.next;
                }
                cur = cur.next;
            }

        }
        if(end != null){
            end.next = null;
        }
        return ret;
    }

    public int numTrees(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 1;
        dp[1] = 1;
        for(int i = 2; i <= n; i++){
            for(int j = 1; j <= i; j++){
                dp[i] += (dp[j - 1] * dp[i - j]);
            }
        }
        for(int e : dp){
            System.out.print(""+ e + ",");
        }
        return dp[n];
    }

    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- > 0){
            int n = sc.nextInt();
            int k = sc.nextInt();
            int[] h = new int[n];
            int[] f = new int[n];
            int[] sum = new int[n];
            for(int i = 0; i < n; i++){
                f[i] = sc.nextInt();
                if(i == 0){
                    sum[0] = f[0];
                }else{
                    sum[i] = sum[i - 1] + f[i];
                }
            }
            for(int i = 0; i < n; i++){
                h[i] = sc.nextInt();
            }
            int[] len = new int[n];
            len[n - 1] = 1;
        }
    }

    public long minimumRemoval(int[] beans) {
        int[] arr = new int[beans.length];
        for(int i = 0; i < beans.length; i++){
            arr[i] = beans[i];
        }
        Arrays.sort(arr);
        int n = arr.length;
        long[] sum = new long[n + 1];
        for(int i = 1; i < sum.length; i++){
            sum[i] = sum[i - 1] + arr[i - 1];
        }

        long cnt = 0;
        long ret = Long.MAX_VALUE;
        for(int i = 0; i < n; i++){
            long tmp = sum[n] - sum[i] - (arr[i] * (long)(n - i)) + cnt;
            ret = Math.min(ret, tmp + cnt);
            cnt += arr[i];
        }
        return ret;
    }

    public int minCost(int[][] costs) {
        int n = costs.length;
        int[][] dp = new int[n][3];
        dp[0][0] = costs[0][0];
        dp[0][1] = costs[0][1];
        dp[0][2] = costs[0][2];
        for(int i = 1; i < n; i++){
            dp[i][0] = costs[i][0] + Math.min(dp[i-1][1], dp[i-1][2]);
            dp[i][1] = costs[i][1] + Math.min(dp[i-1][0], dp[i-1][2]);
            dp[i][2] = costs[i][2] + Math.min(dp[i-1][1], dp[i-1][0]);
        }
        return Math.min(dp[n-1][0],Math.min(dp[n-1][1],dp[n-1][2]));
    }

    public static void main(String[] args) {
        test_2024_01_08 test = new test_2024_01_08();
        test.minimumRemoval(new int[]{4,1,6,5});
    }
}
